package com.example.servlet.studentServlet;

import com.example.dao.StudentDao;
import com.example.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/studentupdate")
public class studentUpdate  extends HttpServlet {
    private StudentDao studentDao = new StudentDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
//        int idd = Integer.parseInt(request.getParameter("id"));
//        Student id = studentDao.getStudentById(idd);
        try {
            // 获取表单数据
            int id = Integer.parseInt(request.getParameter("id"));
            String studentId = request.getParameter("studentId");
            String name = request.getParameter("name");
            String grade = request.getParameter("grade");
            String classes = request.getParameter("classes");
            String email = request.getParameter("email");
            // 创建学生对象
            Student student = new Student();
            student.setId(id);
            student.setStudentId(studentId);
            student.setName(name);
            student.setGrade(grade);
            student.setClasses(classes);
            student.setEmail(email);
            // 更新学生信息
            if (studentDao.updateStudent(student)) {
                response.sendRedirect("index.jsp");
            } else {
                request.setAttribute("error", "更新失败");
                request.setAttribute("student", student);
                request.getRequestDispatcher("updateStudent.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("index.jsp");
        }
    }
}
